/**
 * HOMER Core - an open and flexible OSGi-based software platform for SmartHome and AAL applications
 *
 * Copyright (C) 2011-2013 AIT Austrian Institute of Technology GmbH - <http://www.ait.ac.at>
 * Contact: Thomas Fuxreiter <thomas.fuxreiter@ait.ac.at>
 *
 * This file is part of HOMER Core.
 *
 * HOMER Core is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * HOMER Core is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package at.aal2aac.eventbusclient;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;



/**
 * Class providing the Json serializer and deserialzer for objectes embedded in
 * an {@link EventbusMessage}.
 * 
 * @author kropfj
 * 
 */
public final class JsonUtils {


	private static GsonBuilder builder = new GsonBuilder();;
	
	private static Gson gson;
	
	private JsonUtils() {
		//
	}

	static {
		builder.registerTypeAdapter(EventbusMessage.class, new EventbusMessageGsonAdapter());
		gson = builder.create();
	}

	/**
	 * Serializes an {@link EventbusMessage} with an embedded object to a
	 * JsonString.
	 * 
	 * @param msg
	 *            - the {@link EventbusMessage}
	 * @return
	 */
	public static String toJsonString(final EventbusMessage<?> msg) {
		return gson.toJson(msg);

	}
	
	/**
	 * Deserializes a JsonString to an {@link EventbusMessage} with an embedded
	 * object.
	 * 
	 * @param json
	 *            - the Json String
	 * @return - the EventbusMessage
	 */
	public static EventbusMessage<?> toEventbusMessage(final String json) {
		return gson.fromJson(json, EventbusMessage.class);

	}

}
